<!DOCTYPE html>
<html lang="en">
<head xmlns:th="http://www.thymeleaf.org">
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/css/bootstrap.min.css">
    <script src="/js/jquery.js"></script>
</head>
<body>
<div class="container-fluid">
    <h1> 学生信息表</h1>
    <!-- Button trigger modal -->
    <form class="form-inline">
    <div class="form-group">
        <input type="text" class="form-control" id="name1" name="name" placeholder="请输入需要查询的姓名">
        <button type="button" class="btn btn-primary" onclick="readstudent();">查询</button>
        <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal" onclick="preAdd();">新增学生</button>
    </div>
    </form>
    <table class="table table-bordered">
        <thead>
        <tr>
            <th>编号</th>
            <th>姓名</th>
            <th>学号</th>
            <th>性别</th>
            <th>年龄</th>
            <th>成绩</th>
            <th style="width: 100px">操作</th>
        </tr>
        </thead>
        <tbody id="studentTb">
        </tbody>
    </table>

</div>
<div class="modal fade" tabindex="-1" role="dialog" id="myModal">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">学生信息</h4>
            </div>
            <div class="modal-body">
                <form id="formstudent">
                    <input hidden="hidden" id="id" name="id"/>
                    <div class="form-group">
                        <label for="name">姓名</label>
                        <input type="text" class="form-control" id="name" name="name" placeholder="Name">
                    </div>
                    <div class="form-group">
                        <label for="number">学号</label>
                        <input type="text" class="form-control" id="number" name="numbers" placeholder="Number">
                    </div>
                    <div class="form-group">
                        <label for="password">密码</label>
                        <input type="password" class="form-control" id="password" name="password" placeholder="Password">
                    </div>
                    <div class="form-group">
                        <label for="sex">性别</label>
                        <select class="form-control" id="sex" name="sex">
                            <option value="0">保密</option>
                            <option value="1">男</option>
                            <option value="2">女</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="age">年龄</label>
                        <input type="text" class="form-control" id="age" name="age" placeholder="Age">
                    </div>
                    <div class="form-group">
                        <label for="score">成绩</label>
                        <input type="text" class="form-control" id="score" name="score" placeholder="成绩">
                    </div>
                </form>
                <!--表单结束-->
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" onclick="saveaddstudent();">保存</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<script src="/js/bootstrap.min.js"></script>
<script type="text/javascript">
    function preAdd(){
        $("#id").val(0);//提示新增
        $("#name").val("");
        $("#number").val("");
        $("#password").val("");
        $("#sex").val("0");
        $("#age").val("");
        $("#score").val("");
    }
    function readstudent(){
        var name1=$("#name1").val();
        if (name1=="")
        {
            loadTable();
        }
        else
        {
            $.ajax({
                url: "/webapi/student/read/"+name1,
                method:"get",
            }).done(function (rs) {
                var len=rs.length;
                var html="";
                for (var i = 0; i < len; i++) {
                    var item=rs[i];
                    html+="<tr>"
                        +"<td>"+item.id+"</td>"
                        +"<td>"+item.name+"</td>"
                        +"<td>"+item.numbers+"</td>"
                        +"<td>"+item.sex+"</td>"
                        +"<td>"+item.age+"</td>"
                        +"<td>"+item.score+"</td>"
                        +"<td><a href='#' onclick='editstudent("+item.id+");'>编辑</a> <a href='#' onclick='deletestudent("+item.id+");'>删除</a></td>"
                        +"</tr>"
                }
                $("#studentTb").html(html);
            })
        }
    }
    //新增或者更新
    function saveaddstudent(){
        var data=$("#formstudent").serialize();
        var id=$("#id").val();
        if(id<1)
        {

            //新增
            $.ajax({
                url: "/webapi/student/insert",
                method:"post",
                data:data
            }).done(function(){
                loadTable();
                $("#myModal").modal("hide");
            })
        }
        else
        {
            //更新
            $.ajax({
                url: "/webapi/student/update",
                method:"put",
                data:data
            }).done(function(){
                loadTable();
                $("#myModal").modal("hide");
            })
        }
    }
    function deletestudent(id){
        if (confirm("正在进行删除操作，确定吗？")){
            $.ajax({
                url: "/webapi/student/delete/"+id,
                method:"delete",
            }).done(function(){
                loadTable();
            })
        }//end if

    }
    function editstudent(id){
        $("#myModal").modal("show");
        $.ajax({
            url:"/webapi/student/get/"+id
        }).done(function (rs){
            $("#id").val(rs.id);
            $("#name").val(rs.name);
            $("#number").val(rs.numbers);
            $("#password").val(rs.password);
            $("#sex").val(rs.sex);
            $("#age").val(rs.age);
            $("#score").val(rs.score);
        })
    }
    function loadTable(){
        $.ajax({
            url:"/webapi/student/list"
        }).done(function (rs) {
            var len=rs.length;
            var html="";
            for (var i = 0; i < len; i++) {
                var item=rs[i];
                html+="<tr>"
                    +"<td>"+item.id+"</td>"
                    +"<td>"+item.name+"</td>"
                    +"<td>"+item.numbers+"</td>"
                    +"<td>"+item.sex+"</td>"
                    +"<td>"+item.age+"</td>"
                    +"<td>"+item.score+"</td>"
                    +"<td><a href='#' onclick='editstudent("+item.id+");'>编辑</a> <a href='#' onclick='deletestudent("+item.id+");'>删除</a></td>"
                    +"</tr>"
            }
            $("#studentTb").html(html);
        })
    }
    $(function (){
        loadTable();
    })
</script>
</body>
</html>